#include <string.h>
#include <stdlib.h>

#include "lib.h"
#include "rlsql.h"
#include "rlround.h"
#include "rlbet.h"
#include "odbc.h"
#include "missinggamenums.h"

#define ROULETTE_GAMEID			4	// Class/Player limits gameId

RouletteSQL::RouletteSQL(const String& DBDSN, const String& DBLogin, const String& DBPassword)
	: TrackSQL(DBDSN, DBLogin, DBPassword)
{
#ifdef USE_MISSINGGAMENUMS
	//missing game nums deleted in TrackSQL::~TrackSQL
	missinggamenums = new MissingGameNums(this, "pub_getTableIdentityRoulette");
#endif
}


int32
RouletteSQL::spin(int32 casinoId,
	PID pid, uint8 tableID, bool8 forMoney, SESSIONID sessionId,
	int32 compFactor,
	uint8 spinresult, int32 payout, RouletteRound *round,
	int32* balance, int32* comp)
{
	ODBCConnection* conn;
	ODBCQuery* query;
	bool8 ok = false;
	RouletteGameID gid;

	conn = getConn();
	if (!conn)
		return NoConnection;

	
	query = conn->query();
	if (query)
	{
		String proc;
		String xml;

		xml = "<bets>";
		
		RouletteBet* bet;
		uint8 i;
		String tmpBetId, tmpBetAmount, tmpXmlElement;

		// Create an insert statement for each bet
		for (i = 0; i < RouletteRound::MAX_BETS; i++)
		{
			bet = round->getBet(i);
			if (bet && bet->getAmount() > 0)
			{
				tmpBetId.sprintf( TEXT("%d"), bet->getBetID());
				tmpBetAmount.sprintf( TEXT("%d"), bet->getAmount());

				tmpXmlElement.sprintf("<bet id=\"%d\" amount=\"%d\" />", bet->getBetID(), bet->getAmount());

				xml += tmpXmlElement;
			}
		}
		xml += "</bets>";
		
		// Create the line to call the stored proc
		query->define("pub_CreateGameRoulette ");
		query->addParam(casinoId);
		query->addParam(pid, PIDLEN);
		query->addParam(tableID, ODBCQuery::BINARY);
		query->addParam((bool8)(forMoney != 0));
		query->addParam(sessionId, ODBCQuery::NOQUOTES);
		query->addParam(compFactor);
		query->addParam(spinresult);
		query->addParam(round->numBets());
		query->addParam(round->totalBet(), ODBCQuery::MONEY);
		query->addParam(payout, ODBCQuery::MONEY);
		query->addParam(xml, ODBCQuery::USEQUOTES);

		proc = query->getQuery();

		//theQuery.replace("!proc!", proc);
		//theQuery.replace("!insert!", insert);
		
		if (query->exec(proc))
		{
			while (query->getRow())
			{
				int32 success;
				ok =
					query->getData(1, &success) &&
					query->getData(2, gid, ROULETTE_GAMEID_SIZE) &&
					query->getData(3, balance, ODBCQuery::MONEY) &&
					query->getData(4, comp);

				if (ok)
				{
					ok = (success == 0);
				}
			}
		}
		else
		{
			ASSERT(0);
		}

		delete query;
	}
	delete conn;

#ifdef USE_MISSINGGAMENUMS
	if (ok)
	{
		int32 gamenum = atol(gid);
		if (missinggamenums)
			missinggamenums->setGameNum(gamenum);
	}
#endif

	return ok ? NoError : ExecFailed;
}

int32
RouletteSQL::getPlayerLimits(PID pid, uint8 tableID,
	int32* mininside, int32* maxinside, int32* minoutside, int32* maxoutside,
	int32* maxsingleinside, int32* compFactor)
{
	ODBCConnection* conn;
	ODBCQuery* query;
	bool8 ok = false;

	conn = getConn();
	if (!conn)
		return NoConnection;

	query = conn->query();
	if (query)
	{
		query->define("exec pub_lim_getPlayerLimits ");
		query->addParam(pid, PIDLEN);
		query->addParam((int32)ROULETTE_GAMEID);
		query->addParam(tableID, ODBCQuery::BINARY);

		if (query->exec())
		{
			while (query->getRow())
			{
				ok = 
					query->getData(4, mininside, ODBCQuery::MONEY) &&
					query->getData(5, maxinside, ODBCQuery::MONEY) &&
					query->getData(6, minoutside, ODBCQuery::MONEY) &&
					query->getData(7, maxoutside, ODBCQuery::MONEY) &&
					query->getData(8, compFactor) &&
					query->getData(9, maxsingleinside, ODBCQuery::MONEY);
			}
		}
		delete query;
	}
	delete conn;

	return ok ? isGameEnabled(ROULETTE_GAMEID,tableID) : ExecFailed;
}
